package org.jeecg.modules.fk.utils;

//import cn.meteor.module.util.mapper.BeanMapper;
//import cn.meteor.module.util.security.MD5Utils;
//import cn.meteor.module.util.security.RSAUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.math.BigInteger;
import java.security.interfaces.RSAPublicKey;
import java.util.*;

@Slf4j
public class SwyAPIUtils {
    public static String parseCiphertextToPlaintext(String json, String modulusString, String publicExponentString) throws Exception {
//        BigInteger modulusBigInteger = new BigInteger(modulusString);
//        BigInteger publicExponentBigInteger = new BigInteger(publicExponentString);
//        RSAPublicKey rsaPublicKey = RSAUtils.getRSAPublicKeyByModulusAndPublicExponent(modulusBigInteger, publicExponentBigInteger);
//        byte[] cipherBytes = RSAUtils.encrypt(rsaPublicKey, json.getBytes("UTF-8"));
//        String mw = Base64.encodeBase64String(cipherBytes);
//        return mw;
        return null;
    }

    public static Map<String, Object> requestObjectToMapWithSign(Object requestObject, String appSecret) {
        Map<String, Object> map = new HashMap<String, Object>();
//        BeanMapper.mapWithDozer(requestObject, map);// 1.将请求对象数据转换到map中
//        map.remove("sign");// 2.移除sign
//        Object[] keyArr = map.keySet().toArray(); // 3.把所有key 转成数组
//        Arrays.sort(keyArr);// 4.排序数据
//        String joinString = "";// 5循排序过的数据
//        for (Object key : keyArr) {
//            Object value = map.get(key);
//            if (value != null) {
//                joinString += key.toString().trim() + value.toString().trim();
//                System.err.println(key.toString().trim() + "=" + value.toString().trim());
//            }
//        }
//        joinString = appSecret + joinString + appSecret;
//        System.err.println("md5加密前" + joinString);
//        try {
//            String sign = MD5Utils.md5Digest(joinString).toUpperCase();
//            map.put("sign", sign);
//        } catch (Exception e) {
//            log.error("requestObjectToMap中md5Digest出现异常：", e);
//        }
        return map;
    }

    public static List<NameValuePair> paramMapToNameValuePairList(Map<String, Object> paramMap) {
        List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
        for (String key : paramMap.keySet()) {
            Object value = paramMap.get(key);
            if (value != null) {
                nameValuePairList.add(new BasicNameValuePair(key, "" + value));
            }
        }
        return nameValuePairList;
    }

}
